# -*- coding: utf-8 -*-
"""
 ----------------------------------------
|File Name:     jsbn
|Author:        WYT
|date:          2021/7/2
 ----------------------------------------
|  Change Activity:
|                   2021/7/2:
-----------------------------------------
"""
import numbers


class Jsbn(object):

    canary = 0xdeadbeefcafe
    j_lm = ((canary&0xffffff) == 0xefcafe)

def nbi():
    return BigInteger(None)

def nbv(i):
    r = nbi()
    r.from_int(i)
    return r


class BigInteger(object):

    def __init__(self, a, b, c):
        if a is not None:
            if isinstance(a, numbers.Number):
                self.from_number(a=None, b=None, c=None)
            elif b is None and type(a) == str:
                self.from_string(a, 256)
            else:
                self.from_string(a, b)

    def nbv(self, i):
        pass

    def from_string(self, s=None, b=None):
        k: int
        if b == 16:
            k = 4
        elif b == 8:
            k = 3
        elif b == 256:
            k = 8
        elif b == 2:
            k = 1
        elif b == 32:
            k = 5
        elif b == 4:
            k = 2
        else:
            self.from_radix(s, b)
            return

    def from_radix(self):
        pass

    def op_or(self, x, y):
        return x|y

    def from_number(self, a=None, b=None, c=None):
        if isinstance(b, numbers.Number):
            if a < 2:
                self.from_int(1)
            else:
                self.from_number(a, c)
                if not self.test_bit(a - 1):
                    self.bitwise_to(BigInteger.ONE.shift_left(a-1), self.op_or)
                if self.is_even():
                    self.d_add_offset(1, 0)
                while not self.is_probable_prime(b):
                    self.d_add_offset(2, 0)
                    if self.bit_length() > a:
                        self.sub_to(BigInteger.ONE.shift_left(a-1))
        else:
            x = []
            t = a & 7
            length = (a >> 3) + 1
            for i in range(length):
                x[i] = 0
            b.next_bytes(x)
            if t > 0:
                x[0] &= ((1 << t) - 1)
            else:
                x[0] = 0
            self.from_string(x , 256)



BigInteger.ZERO = nbv(0)
BigInteger.ONE = nbv(1)
